using System;
using System.Linq;
using LastFmLib.API20.Album;
using LastFmLib.API20.Artist;
using LastFmLib.API20.Types;
using StaffWarsFm.Lib.Data;

namespace StaffWarsFm.Questions
{
    public class AlbumQuestionService : QuestionServiceBase
    {
        [Question]
        public Question GetAlbumYearQuestion(ArtistInfo artist)
        {
            var album = GetRandomAlbum(artist);

            return new Question 
            {
                ImageUrl = artist.GetLargestImageUrl().ToString(),
                Text = string.Format("What album did {0} release in {1}?", artist.Name, album.ReleaseDate.Year),
                Answers = GetAnswers(
                    album, 
                    a => new Answer
                    {
                        ImageUrl = a.Default(r => r.GetLargestImageUrl().ToString(), ""),
                        Text = a.Default(r => r.AlbumName, "")
                    }
                )
            };
        }

        [Question]
        public Question GetNumTracksQuestion(ArtistInfo artist)
        {
            var album = GetRandomAlbum(artist);

            return new Question
            {
                ImageUrl = artist.GetLargestImageUrl().ToString(),
                Text = string.Format("How many tracks does {0} by {1} have?", album.AlbumName, artist.Name),
                Answers = GetAnswers(
                    album, 
                    a => new Answer
                    {
                         ImageUrl = "", 
                         Text = a.Default(r => r.Tracks.Count.ToString(), "")
                    }
                )
            };
        }

        [Question]
        public Question GetWhatTrackOnAlbumQuestion(ArtistInfo artist)
        {
            var album = GetRandomAlbum(artist);

            return new Question
            {
                ImageUrl = artist.GetLargestImageUrl().ToString(),
                Text = string.Format("What album, by {0}, is the track {1} on?"),
                Answers = GetAnswers(
                    album, 
                    a => new Answer
                    {
                        ImageUrl = "",
                        Text = a.Default(r => r.AlbumName, "")
                    }
                )
            };
        }

        public AlbumInfo GetRandomAlbum(ArtistInfo artist)
        {
            var getTopAlbums = new ArtistGetTopAlbums(artist.Name);

            getTopAlbums.AuthData = Util.GetAuthData();
            getTopAlbums.Start();
            
            if(getTopAlbums.Succeeded)
            {
                var albums = getTopAlbums.Result.Take(5).ToArray();
                var album = albums[Util.Random(albums.Length)];
                return GetAlbumInfo(album.MbId);
            }

            throw new Exception("There once was a plumber from Lee. Who was plumbing his girl by the sea. She said Stop your plumbing, There's somebody coming! Said the plumber still plumbing... It's me!");
        }

        public AlbumInfo GetAlbumInfo(Guid mbid)
        {
            if(Cache<AlbumInfo>.Contains(a => a.MbId.ToString() == mbid.ToString()))
            {
                return Cache<AlbumInfo>.Get(a => a.MbId.ToString() == mbid.ToString()).First();
            }

            var getAlbumInfo = new AlbumGetInfo(mbid, true);

            getAlbumInfo.AuthData = Util.GetAuthData();
            getAlbumInfo.Start();

            if (getAlbumInfo.Succeeded)
            {
                return getAlbumInfo.Result;
            }

            throw new Exception("I had me a wench from East Broint. Who bade me her skin to anoint. The girl had arthritis. And so I decided. She wouldn't mind one more stiff joint.");
        }
    }
}
